package com.yuantu.judiciary.model.word;


import lombok.Data;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.InputStream;

/**
 * @author kgwei
 * 图片
 */
@Data
public class WordImage implements WordElement {

    private ImageSourceType sourceType;

    private String imageUrl;

    private Double width;

    private Double height;

    private String imageType;

    /**
     * 设置imageType或url有后缀可以不传
     */
    private Integer imageFormat;

    public static WordImage defaultWordImage(String imageUrl, ImageSourceType sourceType, String imageType) {
        return wordImage(imageUrl, sourceType, imageType, 150d, 150d);
    }

    public static WordImage wordImage(String imageUrl, ImageSourceType sourceType, String imageType, Double width, Double height) {
        WordImage wordImage = new WordImage();
        wordImage.setSourceType(sourceType);
        wordImage.setImageUrl(imageUrl);
        wordImage.setImageType(imageType == null ? "png" : imageType);
        wordImage.setWidth(width == null ? 150d : width);
        wordImage.setHeight(height == null ? 150d : height);
        return wordImage;
    }

    @Override
    public WordElementType getElementType() {
        return WordElementType.IMAGE;
    }

    @Override
    public void addElement(XWPFDocument document) {
        XWPFParagraph p = document.createParagraph();
        imageBuild(p.createRun());
    }

    public void imageBuild(XWPFRun r) {
        try {
            InputStream inputStream;
            switch (sourceType) {
                case DISK:
                    inputStream = LoadImage.loadImageStreamWithPath(imageUrl);
                    break;
                case RESOURCE:
                    inputStream = LoadImage.loadImageStreamWithResources(imageUrl);
                    break;
                default:
                    inputStream = LoadImage.loadImageStreamWithUrl(imageUrl);
            }
            r.addPicture(inputStream, getImageFormat(), imageUrl, Units.toEMU(width), Units.toEMU(height));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Integer getImageFormat() {
        if (this.imageFormat != null) {
            return this.imageFormat;
        }
        if (imageType == null) {
            int index = imageUrl.lastIndexOf(".");
            if (index >= 0 && (index + 1) < imageUrl.length()) {
                imageType = imageUrl.substring(index + 1);
            }
        }
        if (imageType != null) {
            int format = -1;
            switch (imageType) {
                case "emf":
                    format = Document.PICTURE_TYPE_EMF;
                    break;
                case "wmf":
                    format = Document.PICTURE_TYPE_WMF;
                    break;
                case "pict":
                    format = Document.PICTURE_TYPE_PICT;
                    break;
                case "jpeg":
                case "jpg":
                    format = Document.PICTURE_TYPE_JPEG;
                    break;
                case "png":
                    format = Document.PICTURE_TYPE_PNG;
                    break;
                case "dib":
                    format = Document.PICTURE_TYPE_DIB;
                    break;
                case "gif":
                    format = Document.PICTURE_TYPE_GIF;
                    break;
                case "tiff":
                    format = Document.PICTURE_TYPE_TIFF;
                    break;
                case "eps":
                    format = Document.PICTURE_TYPE_EPS;
                    break;
                case "bmp":
                    format = Document.PICTURE_TYPE_BMP;
                    break;
                case "wpg":
                    format = Document.PICTURE_TYPE_WPG;
                    break;
                default:
                    System.err.println("Unsupported picture: " + imageType +
                            ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
                    break;
            }
            this.imageFormat = format;
        }
        return this.imageFormat;
    }
}
